package com.c1.client.dialog.r.util;

import javax.swing.*;
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;

public class ImageUtil {
    private static Image getImageIcon(String urlPath) {
        URL url = null;
        try {
            url = new URL(urlPath);
        } catch (MalformedURLException ignored) {
        }
        if (url == null) {
            return null;
        }
        ImageIcon imageIcon = new ImageIcon(url);
        return imageIcon.getImage();
    }

    public static Image getImageIcon(String urlPath, int width, int height, Component component) {
        Image tempImage = getImageIcon(urlPath);
        if (tempImage == null || tempImage.getWidth(component) == -1 || tempImage.getHeight(component) == -1) {
            return null;
        }
        Image image = tempImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        MediaTracker tracker = new MediaTracker(component);
        tracker.addImage(image, 0);
        try {
            tracker.waitForID(0);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return image;
    }
}
